import util from '../../utils/util.js';
import {
  fansAuth,fansDetailh
} from "../../api/user.js"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    editable:false,
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '粉丝团认证',
    },
    name:"",
    desc:'',
    star_name:"",
    star_id:"",
    putData: {},
    img1: '',
    img2: '',
    img3: '',
    img4: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  getData(){
    fansDetailh().then(res=>{
      this.setData({
        star_name:res.data.star_name,
        name:res.data.name,
        desc:res.data.desc,
        img1:res.data.image_input,
        img2:res.data.id_head,
        img3:res.data.id_national,
        img4:res.data.id_body,
      })
    }).catch(err=>{
      this.setData({
        editable:true
      })
    })
  },
  selectStar(){
    if(!this.data.editable){
      return
    }
    wx.navigateTo({
      url: '/pages/goods_cate_select/goods_cate',
    })
  },
  /**
   * 上传文件
   * 
   */
  uploadpic: function (e) {
    if(!this.data.editable){
      this.preImg(e)
      return
    }
    var that = this;
    util.uploadImageOne('upload/image', function (res) {
      let data = {}
      let url = res.data.url
      switch (e.currentTarget.dataset.key) {
        case 'img1':
          data = {
            'img1': url
          }
          break
        case 'img2':
          data = {
            'img2': url
          }
          break
        case 'img3':
          data = {
            'img3': url
          }
          break
        case 'img4':
          data = {
            'img4': url
          }
          break
      }
      that.setData(data)

    });
  },
  preImg(e){
    let url=''
    switch (e.currentTarget.dataset.key) {
      case 'img1':
        url=this.data.img1
        break
      case 'img2':
        url=this.data.img2
        break
      case 'img3':
        url=this.data.img3
        break
      case 'img4':
        url=this.data.img4
        break
    }
    wx.previewImage({
      urls: [url],
    })
  },
  onCheckData(e) {
    var that = this,
      value = e.detail.value;
      if (!that.data.star_name) return app.Tips({
        title: '请选择所属偶像'
      });
    if (!value.name) return app.Tips({
      title: '请填写粉丝团名称'
    });
    if (!value.desc) return app.Tips({
      title: '请填写粉丝团介绍'
    });
    if (!that.data.img1) return app.Tips({
      title: '请上传粉丝团图片'
    });
    if (!that.data.img2) return app.Tips({
      title: '请上传身份证头像页'
    });
    if (!that.data.img3) return app.Tips({
      title: '请上传身份证警徽页'
    });
    if (!that.data.img4) return app.Tips({
      title: '请上传手持身份证全身照'
    });
    value.star_id = that.data.star_id
    value.image_input = that.data.img1
    value.id_head = that.data.img2
    value.id_national = that.data.img3
    value.id_body = that.data.img4
    that.setData({
      putData:value
    })
    wx.showModal({
     title:'提示',
     content:'请确认信息是否填写正确！',
     success(){
     that.putData()
     }
    })
  },
  putData(){
    wx.showLoading({
      title: '提交中...',
    })
    fansAuth(this.data.putData).then(res=>{
      wx.hideLoading()
      app.Tips({
        title: res.msg
      },{tab:3});
    }).catch(err=>{
      wx.hideLoading()
      wx.showModal({
        title:'提交失败',
        content:err
       })
    })
  }
})